package com.ychong.library.view

import android.content.Context
import android.util.AttributeSet
import androidx.recyclerview.widget.RecyclerView
import com.ychong.library.R

class MaxHeightRecyclerView:RecyclerView {
    private var mHeightMax:Int = 0

    constructor(context:Context):super(context)
    constructor(context: Context,attrs:AttributeSet):super(context,attrs){
        initialize(context,attrs)
    }
    constructor(context: Context,attrs: AttributeSet,defStyleAttr:Int):super(context,attrs,defStyleAttr){
        initialize(context,attrs)
    }
    private fun initialize(context: Context,attrs: AttributeSet){
        val arr = context.obtainStyledAttributes(attrs, R.styleable.maxHeight_MaxHeightRecyclerView)
        mHeightMax = arr.getLayoutDimension(R.styleable.maxHeight_MaxHeightRecyclerView_mMaxHeight,mHeightMax)
        arr.recycle()
    }

    override fun onMeasure(widthSpec: Int, heightSpec: Int) {
        var heightMeasureSpec = heightSpec
        if (mHeightMax>0){
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(mHeightMax,MeasureSpec.AT_MOST)
        }
        super.onMeasure(widthSpec, heightMeasureSpec)
    }
}